﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ProcessCommand;
using MediatR;
using SqlSugar;
using Yitter.IdGenerator;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ProcessHandler
{
    public class AddCraftProcedureCommandHandler : IRequestHandler<AddCraftProcedureCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public AddCraftProcedureCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(AddCraftProcedureCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model = mapper.Map<CraftProcedure>(request);

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.code = APIResultCode.success;
                result.msg = "工序列表添加成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "工序列表添加失败";
            }
            return result;
        }
    }
}
